// Copyright 2013 The Go Circuit Project
// Use of this source code is governed by the license for
// The Go Circuit Project, found in the LICENSE file.
//
// Authors:
//   2013 Petar Maymounkov <p@gocircuit.org>

package ns

import (
	"fmt"
	"io"
	"io/ioutil"
	"reflect"
)

// RequestReader
type RequestReader struct {
	io.Reader
	//intr chan struct{}
}

var typeOfRequestReader = reflect.TypeOf(&RequestReader{})

func (q *RequestReader) Read(p []byte) (int, error) {
	return q.Reader.Read(p)
}

func (q *RequestReader) Close() error {
	return nil
}

func (q *RequestReader) ReadSlice() []byte {
	all, err := ioutil.ReadAll(q.Reader)
	if err != nil {
		panic(err)
	}
	return all
}

func (q *RequestReader) ReadString() string {
	all, err := ioutil.ReadAll(q.Reader)
	if err != nil {
		panic(err)
	}
	return string(all)
}

// ResponseWriter
type ResponseWriter struct {
	io.WriteCloser
}

var typeOfResponseWriter = reflect.TypeOf(&ResponseWriter{})

func (p *ResponseWriter) WriteSlice(s []byte) {
	defer p.WriteCloser.Close()
	p.WriteCloser.Write(s)
}

func (p *ResponseWriter) WriteString(s string) {
	p.WriteCloser.Write([]byte(s))
}

func (p *ResponseWriter) Printf(format string, arg ...interface{}) {
	p.WriteString(fmt.Sprintf(format, arg...))
}

func (p *ResponseWriter) Errorf(format string, arg ...interface{}) {
	s := fmt.Sprintf(format, arg...)
	p.WriteString(s)
	panic(s)
}
